﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using HYView;

namespace Model
{
	[Transaction(TransactionMode.Manual)]
	public class CmdEquipConnToConduit : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Autodesk.Revit.DB.View activeView = commandData.Application.ActiveUIDocument.Document.ActiveView;
			if ((int)activeView.ViewType != 1 && (int)activeView.ViewType != 2 && (int)activeView.ViewType != 4 && (int)activeView.ViewType != 3 && (int)activeView.ViewType != 117)
			{
				Result result = SwitchView.SetUsableView((CmdViewType)115, commandData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			this.m_uiApp = commandData.Application;
			this.GetXMLPath();
			if (this.GetConduitTypeDataByDoc() < 1)
			{
				message = "初始化线管类型失败!";
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (!this.ReadWriteXMLFileItem(false))
			{
				message = "读取历史数据失败!";
				return Autodesk.Revit.UI.Result.Failed;
			}
			UIDocument activeUIDocument = this.m_uiApp.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Transaction transaction = new Transaction(document, "ElecEquipConnToConduit");
			try
			{
				using (ElecEquipConnForm elecEquipConnForm = new ElecEquipConnForm(ref this.m_lstDocConduitTypeData, commandData.Application.ActiveUIDocument.Document))
				{
					elecEquipConnForm.StartPosition = FormStartPosition.CenterParent;
					for (;;)
					{
						transaction.Start();
						Reference reference = activeUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new CmdEquipConnToConduit.ConduitFilter(), "请选择要连接的线管");
						IList<Reference> lstRefEquips = activeUIDocument.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new CmdEquipConnToConduit.ElecConnEquipFilter(), "请选择要连接的电气设备");
						Conduit elemConduit = (Conduit)this.GetDocElementByReference(reference);
						elecEquipConnForm.ElemConduit = elemConduit;
						if (elecEquipConnForm.ShowDialog() != DialogResult.OK)
						{
							break;
						}
						this.ExtendConduitByGroupEquips(document, reference, lstRefEquips);
						ConduitType conduitTypeSel = elecEquipConnForm.ConduitTypeSel;
						if (this.DoConnect(reference, lstRefEquips, conduitTypeSel, CmdEquipConnToConduit.MM2Feet(elecEquipConnForm.ConduitRadius)))
						{
							transaction.Commit();
						}
						else
						{
							transaction.RollBack();
						}
					}
					transaction.RollBack();
				}
			}
			catch (Exception ex)
			{
				string message2 = ex.Message;
				transaction.RollBack();
			}
			this.ReadWriteXMLFileItem(true);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private int GetConduitTypeDataByDoc()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_uiApp.ActiveUIDocument.Document);
			filteredElementCollector.OfClass(typeof(ConduitType));
			this.m_lstDocConduitTypeData.Clear();
			foreach (Element element in filteredElementCollector.ToElements())
			{
				ConduitType elem = (ConduitType)element;
				SConduitTypeData item = new SConduitTypeData(16.0, elem);
				if (!this.m_lstDocConduitTypeData.Contains(item))
				{
					this.m_lstDocConduitTypeData.Add(item);
				}
			}
			return this.m_lstDocConduitTypeData.Count;
		}

		private Element GetDocElementByReference(Reference refObj)
		{
			if (this.m_uiApp != null)
			{
				return this.m_uiApp.ActiveUIDocument.Document.GetElement(refObj);
			}
			return null;
		}

		private string GetXMLPath()
		{
			this.m_strXMLPath = Path.GetTempPath() + CmdEquipConnToConduit.s_strXMLFileName;
			if (!File.Exists(this.m_strXMLPath))
			{
				XmlDocument xmlDocument = new XmlDocument();
				XmlNode newChild = xmlDocument.CreateNode(XmlNodeType.Element, CmdEquipConnToConduit.s_strXMLRootNodeName, null);
				xmlDocument.AppendChild(newChild);
				XmlDeclaration newChild2 = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
				xmlDocument.InsertBefore(newChild2, xmlDocument.DocumentElement);
				xmlDocument.Save(this.m_strXMLPath);
			}
			return this.m_strXMLPath;
		}

		private bool ReadWriteXMLFileItem(bool bSave = true)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				if (bSave)
				{
					xmlDocument.Load(this.m_strXMLPath);
					foreach (SConduitTypeData sconduitTypeData in this.m_lstDocConduitTypeData)
					{
						bool flag = true;
						string text = string.Format("{0:F0}", sconduitTypeData.Radius);
						XmlNode documentElement = xmlDocument.DocumentElement;
						foreach (object obj in documentElement.ChildNodes)
						{
							XmlNode xmlNode = (XmlNode)obj;
							XmlNode namedItem = xmlNode.Attributes.GetNamedItem(CmdEquipConnToConduit.s_strXMLAttributeRadius);
							XmlNode namedItem2 = xmlNode.Attributes.GetNamedItem(CmdEquipConnToConduit.s_strXMLAttributeType);
							if (string.Compare(sconduitTypeData.Name, namedItem2.Value, true) == 0)
							{
								namedItem.Value = text;
								flag = false;
								break;
							}
						}
						if (flag)
						{
							XmlNode xmlNode2 = xmlDocument.CreateNode(XmlNodeType.Element, CmdEquipConnToConduit.s_strXMLRecordName, null);
							XmlAttribute xmlAttribute = xmlDocument.CreateAttribute(CmdEquipConnToConduit.s_strXMLAttributeType);
							xmlAttribute.InnerText = sconduitTypeData.Name;
							xmlNode2.Attributes.Append(xmlAttribute);
							xmlAttribute = xmlDocument.CreateAttribute(CmdEquipConnToConduit.s_strXMLAttributeRadius);
							xmlAttribute.InnerText = text;
							xmlNode2.Attributes.Append(xmlAttribute);
							documentElement.AppendChild(xmlNode2);
						}
					}
					xmlDocument.Save(this.m_strXMLPath);
				}
				else
				{
					xmlDocument.Load(this.m_strXMLPath);
					foreach (object obj2 in xmlDocument.DocumentElement.ChildNodes)
					{
						XmlNode xmlNode3 = (XmlNode)obj2;
						XmlNode namedItem3 = xmlNode3.Attributes.GetNamedItem(CmdEquipConnToConduit.s_strXMLAttributeRadius);
						XmlNode namedItem4 = xmlNode3.Attributes.GetNamedItem(CmdEquipConnToConduit.s_strXMLAttributeType);
						foreach (SConduitTypeData sconduitTypeData2 in this.m_lstDocConduitTypeData)
						{
							if (string.Compare(sconduitTypeData2.Name, namedItem4.Value, true) == 0)
							{
								sconduitTypeData2.Radius = double.Parse(namedItem3.Value);
								break;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				TaskDialog.Show("读、写XML文件出现问题:", ex.Message);
				return false;
			}
			return true;
		}

		private bool DoConnect(Reference refConduit, IList<Reference> lstRefEquips, ConduitType type, double dConduitRadius)
		{
			Conduit conduit = (Conduit)this.GetDocElementByReference(refConduit);
			if (conduit == null)
			{
				return false;
			}
			if (lstRefEquips.Count < 0)
			{
				return false;
			}
			LocationCurve locationCurve = conduit.Location as LocationCurve;
			Line line = (Line)locationCurve.Curve;
			Document document = conduit.Document;
			List<PointSort> list = new List<PointSort>();
			for (int i = 0; i < lstRefEquips.Count; i++)
			{
				FamilyInstance familyInstance = (FamilyInstance)this.GetDocElementByReference(lstRefEquips[i]);
				XYZ elecEquipConnectorPos = this.GetElecEquipConnectorPos(familyInstance);
				double num = 0.0;
				XYZ xyz = this.IsValidEquip2Conduit(conduit, elecEquipConnectorPos, ref num);
				if (xyz != null)
				{
					SortableEquip sortableEquip = null;
					if (this.CreateDataInfo(line, familyInstance, out sortableEquip))
					{
						Line line2;
						if (xyz.IsAlmostEqualTo(sortableEquip.Position))
						{
							line2 = Line.CreateUnbound(sortableEquip.Position, XYZ.BasisZ);
						}
						else
						{
							line2 = Line.CreateUnbound(sortableEquip.Position, xyz - sortableEquip.Position);
						}
						List<SortableEquip> list2 = new List<SortableEquip>();
						List<SortableEquip> list3 = new List<SortableEquip>();
						list2.Add(sortableEquip);
						bool flag = this.IsUpConduit(line, sortableEquip.Position);
						int j = i + 1;
						while (j < lstRefEquips.Count)
						{
							FamilyInstance familyInstance2 = (FamilyInstance)this.GetDocElementByReference(lstRefEquips[j]);
							XYZ elecEquipConnectorPos2 = this.GetElecEquipConnectorPos(familyInstance2);
							double num2 = 0.0;
							if (this.IsValidEquip2Conduit(conduit, elecEquipConnectorPos2, ref num2) == null)
							{
								lstRefEquips.RemoveAt(j);
							}
							else if (num - CmdEquipConnToConduit.s_dEquipOffsetEpsilon - num2 <= SortableEquip.s_dEpsilon && num2 - (num + CmdEquipConnToConduit.s_dEquipOffsetEpsilon) <= SortableEquip.s_dEpsilon)
							{
								SortableEquip sortableEquip2 = null;
								if (this.CreateDataInfo(line, familyInstance2, out sortableEquip2))
								{
									sortableEquip2.Position = line2.Project(sortableEquip2.Position).XYZPoint;
									if (this.IsUpConduit(line, sortableEquip2.Position) == flag)
									{
										list2.Add(sortableEquip2);
									}
									else
									{
										list3.Add(sortableEquip2);
									}
								}
								lstRefEquips.RemoveAt(j);
							}
							else
							{
								j++;
							}
						}
						list2.Sort();
						list3.Sort();
						PointSort pointSort = new PointSort(xyz, num);
						List<Conduit> list4 = null;
						if (this.ConnEquips(conduit, ref list2, document, type, out list4, dConduitRadius))
						{
							foreach (Conduit conduit2 in list4)
							{
								if (conduit2 != null)
								{
									pointSort.ListConduits.Add(conduit2);
								}
							}
						}
						List<Conduit> list5 = null;
						if (this.ConnEquips(conduit, ref list3, document, type, out list5, dConduitRadius))
						{
							foreach (Conduit conduit3 in list5)
							{
								if (conduit3 != null)
								{
									pointSort.ListConduits.Add(conduit3);
								}
							}
						}
						list.Add(pointSort);
						foreach (SortableEquip sortableEquip3 in list2)
						{
							sortableEquip3.CreateFitting(document, type);
						}
						foreach (SortableEquip sortableEquip4 in list3)
						{
							sortableEquip4.CreateFitting(document, type);
						}
					}
				}
			}
			if (list.Count < 1)
			{
				return false;
			}
			list.Sort(new PointComparer());
			SubTransaction subTransaction = new SubTransaction(document);
			try
			{
				List<Conduit> list6 = new List<Conduit>();
				subTransaction.Start();
				XYZ xyz2 = line.GetEndPoint(0);
				XYZ endPoint = line.GetEndPoint(1);
				XYZ xyz3 = xyz2;
				XYZ xyz4 = endPoint;
				Connector conduitConn = this.GetConduitConn(conduit, endPoint);
				Connector connector = connector = this.GetFittingConn2Conduit(conduit, conduitConn);
				PointSort pointSort2 = null;
				bool flag2 = true;
				double dRadius = conduit.get_Parameter(BuiltInParameter.RBS_CONDUIT_DIAMETER_PARAM).AsDouble();
				ElementId typeId = conduit.GetTypeId();
				int num3 = 0;
				bool flag3 = false;
				foreach (PointSort pointSort3 in list)
				{
					if (xyz3.IsAlmostEqualTo(pointSort3))
					{
						pointSort3.ListConduits.Add(conduit);
						num3 = 1;
					}
					else if (xyz4.IsAlmostEqualTo(pointSort3))
					{
						if (list.Count == 1)
						{
							pointSort3.ListConduits.Add(conduit);
						}
						else if (list.Count == 2 && num3 == 1)
						{
							pointSort3.ListConduits.Add(conduit);
						}
						else
						{
							Conduit item = Conduit.Create(document, typeId, xyz2, pointSort3, conduit.LevelId);
							pointSort3.ListConduits.Add(item);
							pointSort2.ListConduits.Add(item);
							list6.Add(item);
						}
						flag3 = true;
					}
					else
					{
						Conduit item2;
						if (flag2)
						{
							Line curve = Line.CreateBound(line.GetEndPoint(0), list[num3]);
							locationCurve.Curve = curve;
							item2 = conduit;
							flag2 = false;
						}
						else
						{
							item2 = Conduit.Create(document, typeId, xyz2, pointSort3, conduit.LevelId);
							list6.Add(item2);
						}
						pointSort3.ListConduits.Add(item2);
						if (pointSort2 != null)
						{
							pointSort2.ListConduits.Add(item2);
						}
						xyz2 = pointSort3;
						pointSort2 = pointSort3;
					}
				}
				if (!flag3)
				{
					Conduit conduit4 = Conduit.Create(document, typeId, xyz2, endPoint, conduit.LevelId);
					list6.Add(conduit4);
					pointSort2.ListConduits.Add(conduit4);
					if (connector != null)
					{
						Connector conduitConn2 = this.GetConduitConn(conduit4, endPoint);
						connector.ConnectTo(conduitConn2);
					}
				}
				subTransaction.Commit();
				foreach (Conduit elemConduit in list6)
				{
					this.SetConduitRadius(elemConduit, dRadius);
				}
			}
			catch (Exception)
			{
				subTransaction.RollBack();
			}
			foreach (PointSort pointSort4 in list)
			{
				pointSort4.CreateFitting(document, type);
			}
			return true;
		}

		private void ExtendConduitByGroupEquips(Document doc, Reference refConduit, IList<Reference> lstRefEquips)
		{
			LocationCurve locationCurve = (doc.GetElement(refConduit) as Conduit).Location as LocationCurve;
			Line line = locationCurve.Curve as Line;
			if (line == null)
			{
				return;
			}
			Line line2 = line.Clone() as Line;
			line2.MakeUnbound();
			List<FamilyInstance> list = new List<FamilyInstance>();
			List<Reference> list2 = new List<Reference>(lstRefEquips);
			for (int i = 0; i < list2.Count; i++)
			{
				FamilyInstance familyInstance = (FamilyInstance)this.GetDocElementByReference(list2[i]);
				XYZ elecEquipConnectorPos = this.GetElecEquipConnectorPos(familyInstance);
				IntersectionResult intersectionResult = line2.Project(elecEquipConnectorPos);
				XYZ xyzpoint = intersectionResult.XYZPoint;
				double parameter = intersectionResult.Parameter;
				SortableEquip sortableEquip = null;
				if (this.CreateDataInfo(line, familyInstance, out sortableEquip))
				{
					Line line3;
					if (xyzpoint.IsAlmostEqualTo(sortableEquip.Position))
					{
						line3 = Line.CreateUnbound(sortableEquip.Position, XYZ.BasisZ);
					}
					else
					{
						line3 = Line.CreateUnbound(sortableEquip.Position, xyzpoint - sortableEquip.Position);
					}
					List<SortableEquip> list3 = new List<SortableEquip>();
					List<SortableEquip> list4 = new List<SortableEquip>();
					list3.Add(sortableEquip);
					bool flag = this.IsUpConduit(line, sortableEquip.Position);
					int j = i + 1;
					while (j < list2.Count)
					{
						FamilyInstance familyInstance2 = (FamilyInstance)this.GetDocElementByReference(list2[j]);
						XYZ elecEquipConnectorPos2 = this.GetElecEquipConnectorPos(familyInstance2);
						IntersectionResult intersectionResult2 = line2.Project(elecEquipConnectorPos2);
						XYZ xyzpoint2 = intersectionResult2.XYZPoint;
						double parameter2 = intersectionResult2.Parameter;
						if (parameter - CmdEquipConnToConduit.s_dEquipOffsetEpsilon - parameter2 <= SortableEquip.s_dEpsilon && parameter2 - (parameter + CmdEquipConnToConduit.s_dEquipOffsetEpsilon) <= SortableEquip.s_dEpsilon)
						{
							SortableEquip sortableEquip2 = null;
							if (this.CreateDataInfo(line, familyInstance2, out sortableEquip2))
							{
								sortableEquip2.Position = line3.Project(sortableEquip2.Position).XYZPoint;
								if (this.IsUpConduit(line, sortableEquip2.Position) == flag)
								{
									list3.Add(sortableEquip2);
								}
								else
								{
									list4.Add(sortableEquip2);
								}
							}
							list2.RemoveAt(j);
						}
						else
						{
							j++;
						}
					}
					list3.Sort();
					list4.Sort();
					if (list3.Count > 0)
					{
						list.Add(list3[0].FiEquip);
					}
					if (list4.Count > 0)
					{
						list.Add(list4[0].FiEquip);
					}
				}
			}
			List<PointSort> list5 = new List<PointSort>();
			foreach (FamilyInstance fi in list)
			{
				XYZ elecEquipConnectorPos3 = this.GetElecEquipConnectorPos(fi);
				IntersectionResult intersectionResult3 = line2.Project(elecEquipConnectorPos3);
				double parameter3 = intersectionResult3.Parameter;
				PointSort item = new PointSort(intersectionResult3.XYZPoint, parameter3);
				list5.Add(item);
			}
			list5.Sort(new PointComparer());
			PointSort pointSort = list5[0];
			PointSort pointSort2 = list5[list5.Count - 1];
			XYZ xyz = line.GetEndPoint(0);
			XYZ xyz2 = line.GetEndPoint(1);
			double parameter4 = line2.Project(xyz).Parameter;
			double parameter5 = line2.Project(xyz2).Parameter;
			if (parameter5 - parameter4 <= 1E-06)
			{
				if (parameter4 - pointSort2.Param <= 1E-06)
				{
					xyz = pointSort2;
				}
				if (pointSort.Param - parameter5 <= 1E-06)
				{
					xyz2 = pointSort;
				}
			}
			else
			{
				if (pointSort.Param - parameter4 <= 1E-06)
				{
					xyz = pointSort;
				}
				if (parameter5 - pointSort2.Param <= 1E-06)
				{
					xyz2 = pointSort2;
				}
			}
			SubTransaction subTransaction = new SubTransaction(doc);
			try
			{
				subTransaction.Start();
				locationCurve.Curve = Line.CreateBound(xyz, xyz2);
				subTransaction.Commit();
			}
			catch (Exception)
			{
				subTransaction.RollBack();
			}
		}

		private bool ConnEquips(Conduit elemConduit, ref List<SortableEquip> lstConnEquips, Document doc, ConduitType type, out List<Conduit> lstLastConduits, double dConduitRadius)
		{
			lstLastConduits = new List<Conduit>();
			Conduit conduit = null;
			if (elemConduit == null || lstConnEquips.Count < 1)
			{
				return false;
			}
			SubTransaction subTransaction = new SubTransaction(doc);
			try
			{
				subTransaction.Start();
				Line line = (Line)(elemConduit.Location as LocationCurve).Curve;
				Line line2 = Line.CreateUnbound(line.Origin, line.Direction);
				XYZ position = lstConnEquips[0].Position;
				IntersectionResult intersectionResult = line2.Project(position);
				double z = line.Origin.Z;
				if (lstConnEquips.Count == 1)
				{
					if (!position.IsAlmostEqualTo(intersectionResult.XYZPoint))
					{
						conduit = Conduit.Create(doc, type.Id, position, intersectionResult.XYZPoint, elemConduit.LevelId);
						lstConnEquips[0].ListConduits.Add(conduit);
					}
					XYZ xyz = position - XYZ.BasisZ * (z - lstConnEquips[0].Elev);
					if (!position.IsAlmostEqualTo(xyz))
					{
						Conduit conduit2 = Conduit.Create(doc, type.Id, position, xyz, elemConduit.LevelId);
						lstConnEquips[0].ListConduits.Add(conduit2);
						if (position.IsAlmostEqualTo(intersectionResult.XYZPoint))
						{
							conduit = conduit2;
						}
					}
				}
				else
				{
					XYZ xyz2 = intersectionResult.XYZPoint - position;
					Line line3 = Line.CreateUnbound(position, xyz2);
					Conduit item = null;
					for (int i = 0; i < lstConnEquips.Count - 1; i++)
					{
						SortableEquip sortableEquip = lstConnEquips[i];
						SortableEquip sortableEquip2 = lstConnEquips[i + 1];
						XYZ xyzpoint = line3.Project(sortableEquip.Position).XYZPoint;
						XYZ xyzpoint2 = line3.Project(sortableEquip2.Position).XYZPoint;
						if (!position.IsAlmostEqualTo(intersectionResult.XYZPoint))
						{
							item = Conduit.Create(doc, type.Id, xyzpoint, xyzpoint2, elemConduit.LevelId);
							sortableEquip.ListConduits.Add(item);
							sortableEquip2.ListConduits.Add(item);
						}
						XYZ xyz3 = xyzpoint - XYZ.BasisZ * (z - sortableEquip.Elev);
						if (!xyzpoint.IsAlmostEqualTo(xyz3))
						{
							Conduit item2 = Conduit.Create(doc, type.Id, xyzpoint, xyz3, elemConduit.LevelId);
							sortableEquip.ListConduits.Add(item2);
						}
					}
					SortableEquip sortableEquip3 = lstConnEquips[lstConnEquips.Count - 1];
					XYZ xyzpoint3 = line3.Project(sortableEquip3.Position).XYZPoint;
					IntersectionResult intersectionResult2 = line2.Project(xyzpoint3);
					if (!xyzpoint3.IsAlmostEqualTo(intersectionResult2.XYZPoint))
					{
						conduit = Conduit.Create(doc, type.Id, xyzpoint3, intersectionResult2.XYZPoint, elemConduit.LevelId);
						if (conduit != null)
						{
							sortableEquip3.ListConduits.Add(conduit);
						}
					}
					XYZ xyz4 = xyzpoint3 - XYZ.BasisZ * (z - sortableEquip3.Elev);
					if (!xyzpoint3.IsAlmostEqualTo(xyz4))
					{
						Conduit item3 = Conduit.Create(doc, type.Id, xyzpoint3, xyz4, elemConduit.LevelId);
						sortableEquip3.ListConduits.Add(item3);
						if (intersectionResult2.Distance < 0.0001)
						{
							lstLastConduits.Add(item3);
							lstLastConduits.Add(item);
							sortableEquip3.IsValid = false;
						}
					}
				}
				if (conduit != null)
				{
					lstLastConduits.Add(conduit);
				}
				subTransaction.Commit();
			}
			catch
			{
				subTransaction.RollBack();
			}
			SubTransaction subTransaction2 = new SubTransaction(doc);
			try
			{
				subTransaction2.Start();
				foreach (SortableEquip sortableEquip4 in lstConnEquips)
				{
					foreach (Conduit elemConduit2 in sortableEquip4.ListConduits)
					{
						this.SetConduitRadius(elemConduit2, dConduitRadius);
					}
				}
				subTransaction2.Commit();
			}
			catch
			{
				subTransaction2.RollBack();
				return false;
			}
			return true;
		}

		private XYZ GetElecEquipConnectorPos(FamilyInstance fi)
		{
			if (fi != null)
			{
				foreach (object obj in fi.MEPModel.ConnectorManager.Connectors)
				{
					Connector connector = (Connector)obj;
					if (connector.Domain == (Autodesk.Revit.DB.Domain)2)
					{
						return connector.Origin;
					}
				}
			}
			return null;
		}

		private Connector GetFittingConn2Conduit(Conduit elemConduit, Connector connOldMainConduitEnd)
		{
			if (elemConduit == null || connOldMainConduitEnd == null)
			{
				return null;
			}
			if (connOldMainConduitEnd.IsConnected)
			{
				ConnectorSetIterator connectorSetIterator = connOldMainConduitEnd.AllRefs.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null)
					{
						FamilyInstance familyInstance = connector.Owner as FamilyInstance;
						if (familyInstance != null)
						{
							ConnectorSetIterator connectorSetIterator2 = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
							while (connectorSetIterator2.MoveNext())
							{
								object obj2 = connectorSetIterator2.Current;
								Connector connector2 = obj2 as Connector;
								ConnectorSetIterator connectorSetIterator3 = connector2.AllRefs.ForwardIterator();
								while (connectorSetIterator3.MoveNext())
								{
									object obj3 = connectorSetIterator3.Current;
									Connector connector3 = obj3 as Connector;
									if (connector3 != null && connector3.Origin.IsAlmostEqualTo(connOldMainConduitEnd.Origin))
									{
										return connector2;
									}
								}
							}
						}
					}
				}
			}
			return null;
		}

		private XYZ IsValidEquip2Conduit(Conduit elemConduit, XYZ ptEquipPos, ref double dParam)
		{
			if (ptEquipPos == null || elemConduit == null)
			{
				return null;
			}
			Line line = (elemConduit.Location as LocationCurve).Curve as Line;
			XYZ xyz = line.Direction.CrossProduct(XYZ.BasisZ);
			XYZ xyz2 = new XYZ(ptEquipPos.X, ptEquipPos.Y, line.Origin.Z);
			Line line2 = Line.CreateUnbound(xyz2, xyz);
			IntersectionResultArray intersectionResultArray = new IntersectionResultArray();
            if ((int)line.Intersect(line2, out intersectionResultArray) == 4 || intersectionResultArray.Size < 1)
			{
				return null;
			}
			IntersectionResult intersectionResult = Line.CreateUnbound(line.Origin, line.Direction).Project(xyz2);
			dParam = intersectionResult.Parameter;
			return intersectionResultArray.get_Item(0).XYZPoint;
		}

		private bool IsUpConduit(Line geLine, XYZ ptTest)
		{
			XYZ xyzpoint = Line.CreateUnbound(geLine.Origin, geLine.Direction).Project(ptTest).XYZPoint;
			XYZ xyz = ptTest - xyzpoint;
			return Math.Abs(geLine.Direction.AngleOnPlaneTo(xyz, XYZ.BasisZ) - Math.PI*.5) <= SortableEquip.s_dEpsilon;
		}

		private bool CreateDataInfo(Line geLineCunduit, FamilyInstance fiEquip, out SortableEquip sData)
		{
			XYZ elecEquipConnectorPos = this.GetElecEquipConnectorPos(fiEquip);
			if (elecEquipConnectorPos == null)
			{
				sData = null;
				return false;
			}
			Curve curve = Line.CreateUnbound(geLineCunduit.Origin, geLineCunduit.Direction);
			XYZ xyz = new XYZ(elecEquipConnectorPos.X, elecEquipConnectorPos.Y, geLineCunduit.Origin.Z);
			IntersectionResult intersectionResult = curve.Project(xyz);
			sData = new SortableEquip(xyz, fiEquip, intersectionResult.Parameter, intersectionResult.Distance);
			return true;
		}

		private Connector GetConduitConn(Conduit elemConduit, XYZ ptPos)
		{
			if (elemConduit != null)
			{
				ConnectorSetIterator connectorSetIterator = elemConduit.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && connector.Origin.IsAlmostEqualTo(ptPos))
					{
						return connector;
					}
				}
			}
			return null;
		}

		private bool SetConduitRadius(Conduit elemConduit, double dRadius)
		{
			Parameter parameter = elemConduit.get_Parameter(BuiltInParameter.RBS_CONDUIT_DIAMETER_PARAM);
			return parameter != null && parameter.Set(dRadius);
		}

		public static double Feet2MM(double dFeetVal)
		{
			return AssistFunc.feetToMM(dFeetVal);
		}

		public static double MM2Feet(double dMMVal)
		{
			return AssistFunc.mmToFeet(dMMVal);
		}

		private UIApplication m_uiApp;

		private List<SConduitTypeData> m_lstDocConduitTypeData = new List<SConduitTypeData>();

		private static string s_strXMLRootNodeName = "ElecEquipConnToConduitData";

		private static string s_strXMLRecordName = "ConduitTypeName2Radius";

		private static string s_strXMLAttributeType = "ConduitType";

		private static string s_strXMLAttributeRadius = "Radius";

		private static string s_strXMLFileName = "ElecEquipConnToConduit.xml";

		private string m_strXMLPath;

		private static double s_dEquipOffsetEpsilon = CmdEquipConnToConduit.MM2Feet(150.0);

		private class ConduitFilter : ISelectionFilter
		{
			public bool AllowElement(Element element)
			{
				Conduit conduit = element as Conduit;
				if (conduit != null)
				{
					LocationCurve locationCurve = conduit.Location as LocationCurve;
					XYZ endPoint = locationCurve.Curve.GetEndPoint(0);
					XYZ endPoint2 = locationCurve.Curve.GetEndPoint(1);
					return Math.Abs(endPoint.Z - endPoint2.Z) <= 1E-06;
				}
				return false;
			}

			public bool AllowReference(Reference refer, XYZ point)
			{
				return false;
			}
		}

		private class ElecConnEquipFilter : ISelectionFilter
		{
			public bool AllowElement(Element elem)
			{
				if (elem != null && elem is FamilyInstance)
				{
					IEnumerator enumerator = (elem as FamilyInstance).MEPModel.ConnectorManager.Connectors.GetEnumerator();
					{
						while (enumerator.MoveNext())
						{
							if (((Connector)enumerator.Current).Domain == (Autodesk.Revit.DB.Domain)2)
							{
								return true;
							}
						}
					}
					return false;
				}
				return false;
			}

			public bool AllowReference(Reference reference, XYZ position)
			{
				return false;
			}
		}
	}
}
